﻿using System.ComponentModel;
using System.Data.Entity;
using System.Linq;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using SH.DataAccess;
using SH.DataAccess.Migrations;
using SH.Foundation;
using SH.Model;

namespace SH.Data.View.ViewModel
{
    public class MainWindowViewModel : Foundation.Model
    {
        public ICollectionView Statuses { get; set; }
        public ICollectionView Regions { get; set; }

        private ICollectionView storehauses;
        public ICollectionView Storehauses
        {
            get { return storehauses; }
            set
            {
                if (storehauses != value)
                {
                    storehauses = value;
                    RaisePropertyChanged("Storehauses");
                }
            }
        }
        public ICollectionView Certificates { get; set; }

        /// <summary>
        /// command
        /// </summary>
        public ICommand Exit { get; private set; }

        public ICommand Delete { get; private set; }

        public ISearch Search { get; set; }

        internal ModelContainer context;

        public MainWindowViewModel()
        {
            // установка соединения
            Database.SetInitializer(new MigrateDatabaseToLatestVersion<ModelContainer, Configuration>());
            context = new ModelContainer();
            context.Database.Initialize(false);

            Search = new Search(this);

            CommandsInit();

            DataInit();
        }

        #region Commands

        private void DeleteImpl()
        {
            var x = (Storehause) Storehauses.CurrentItem;
            if (MessageBox.Show("Ви впевненні, що бажаєте видалити "+x.Edrpou+"?", "Видалення", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                context.Storehauses.Remove(x);
            context.SaveChanges();
            Search.SearchExecute.Execute(null);
        }

        #endregion


        public void CommandsInit()
        {
            Delete = new DelegateCommand((x) => DeleteImpl());
        }

        public void DataInit()
        {
            // Статусы, выборка данных
            var query1 = from vis in context.Statuses
                        select vis;
            Statuses = CollectionViewSource.GetDefaultView(query1.ToList());

            // Регионы, выборка данных
            var query2 = from vis in context.Regions
                         select vis;
            Regions = CollectionViewSource.GetDefaultView(query2.ToList());

            // Склады, выборка данных
            //var query3 = from vis in context.Storehauses
            //            select vis;
            //Storehauses = CollectionViewSource.GetDefaultView(query3.ToList());

            // Сертификаты, выборка данных
            var query4 = from vis in context.Certificates
                         select vis;
            Certificates = CollectionViewSource.GetDefaultView(query4.ToList());
        }

        
    }
}
